import GenerateKeys as gk
import F_function as f

#十六进制转二进制比特串
def Hex2bin(text):
    result = []
    for i in range(len(text)):
        result.extend(f.int2bit(int(text[i],16)))
    return result

#二进制比特串转十六进制
def bin2Hex(text):
    result = []
    q = len(text)//4
    for i in range(q):
        dec = int(text[4*i])*8 + int(text[4*i+1])*4 + int(text[4*i+2])*2 + int(text[4*i+3])*1
        x = hex(dec)[2:].upper()
        result.extend(x)
    rs = ''.join(result)
    return rs

def Encryption(ptext, key):
    text = Hex2bin(ptext)
    keybit = Hex2bin(key)
    keylist = gk.Subkey(keybit)
    # IP置换
    text1 = f.IP(text, 0)
    L = [text1[i] for i in range(32)]
    R = [text1[i] for i in range(32,64)]
    for i in range(16):
        tmp = R
        tmp = f.Extend(tmp)
        tmp = f.Xor(tmp, keylist[i])
        tmp = f.S_replace(tmp)
        tmp = f.P_replace(tmp)
        tmp = f.Xor(tmp, L)
        L = R
        R = tmp
    L,R = R,L
    ctext = L
    ctext.extend(R)
    ctext = f.IP(ctext, 1)
    return bin2Hex(ctext)

def Decryption(ptext, key):
    text = Hex2bin(ptext)
    keybit = Hex2bin(key)
    keylist = gk.Subkey(keybit)
    # IP置换
    text1 = f.IP(text, 0)
    L = [text1[i] for i in range(32)]
    R = [text1[i] for i in range(32,64)]
    for i in range(16):
        tmp = R
        tmp = f.Extend(tmp)
        tmp = f.Xor(tmp, keylist[15-i])
        tmp = f.S_replace(tmp)
        tmp = f.P_replace(tmp)
        tmp = f.Xor(tmp, L)
        L = R
        R = tmp
    L,R = R,L
    ctext = L
    ctext.extend(R)
    ctext = f.IP(ctext, 1)
    return bin2Hex(ctext)

